// pages/operation/classRecord/classRecordDetail/index.js
import {
    Order
} from "../../../../api/api-work/order.js";
import { ClassModel } from "../../../../api/api-work/classModel.js"
import Toast from '@vant/weapp/toast/toast';
const classModel = new ClassModel()
const order = new Order();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        week: "",
        id: "",
        orderNo: "",
        infoData: {},
        tableDatas: [],
        checked: false, //多选按钮
        pageNum: 1,
        pageSize: 10,
        total: 0,
        istriggered: false,
        loadStatus: "",

        showDetail: false,
        detailForm: {
            id: "",
            studentName: "",
            courseName: "",
            detailSelectState: "", // 选中状态  默认到课
            classHourPay: "", // 扣课时数
            description: ""
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const params = JSON.parse(options.param);
        console.log('options---------', params);
        this.setData({
            id: params.id,
            orderNo: params.orderNo,
            week: this.getDayOfWeek(params.classDate)
        })
        // await this.getMumberListByRollCallId()
        // await this.getRollCallDetail()
    },

    async getRollCallDetail() {
        order.getRollCallDetailAPI({ id: this.data.id }).then(res => {

            res.classDate = res.classDate.split(" ")[0]
            this.setData({
                infoData: res
            })
            console.log("hahah: ", this.data.infoData)
        })
    },
    /**
   * 生命周期函数--监听页面显示
   */
    async onShow() {
        this.setData({
            pageNum: 1,
            tableDatas: []
        })
        await this.getRollCallDetail()
        await this.getMumberListByRollCallId()
    },
    //算周几
    getDayOfWeek(dateString) {
        const date = new Date(dateString);
        const days = ['日', '一', '二', '三', '四', '五', '六'];
        return days[date.getDay()];
    },
    //获取课次学员列表
    async getMumberListByRollCallId() {
        order.getMumberListByRollCallIdAPI({
            orderNo: this.data.orderNo,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }).then(res => {

            let rows = this.data.tableDatas;
            rows.push(...res.rows);
            this.setData({
                tableDatas: rows,
                total: res.total
            })
            if (this.data.total == 0) {
                this.setData({
                    // 无数据
                    loadStatus: ""
                })
            } else if (this.data.tableDatas.length < this.data.total) {
                // 还有数据
                this.setData({
                    // 加载更多
                    loadStatus: "-"
                })
            } else {
                // 没有数据了
                this.setData({
                    // 加载更多
                    loadStatus: "nomore"
                })
            }
        })
    },

    // 上拉加载
    getMoreDatas() {
        if (this.data.loadStatus === 'nomore') {
            // 没有更多了
            return;
        }
        this.setData({
            pageNum: this.data.pageNum + 1,
            loadStatus: "loading" // 加载中
        })
        this.getMumberListByRollCallId();
    },

    // 下拉刷新
    refreshMoreDatas() {
        this.setData({
            istriggered: false,
            pageNum: 1,
            tableDatas: []
        })
        this.getMumberListByRollCallId();
    },
    onChange(event) {
        this.setData({
            checked: event.detail,
        });
    },

    // 修改课次信息
    gotoClassCourseEdit() {
        console.log("aaa: ", this.data.infoData)
        wx.navigateTo({
            url: `/pages/operation/class/classCourseEdit/index?id=${this.data.infoData.id}&orderNo=${this.data.infoData.orderNo}`,
        })
    },

    // 添加临时学员
    addTempStudent() {
        wx.navigateTo({
            url: `/pages/operation/class/addTempStudent/index?orderNo=${this.data.infoData.orderNo}&idsStr=${this.data.infoData.courseId}&cofId=${this.data.infoData.cofId}&classDate=${this.data.infoData.classDate}&tid=${this.data.infoData.id}`,
        })
    },

    // 添加补课学员
    addMakeUpStudent() {
        const obj = {
            tid: this.data.infoData.id,
            id: this.data.infoData.courseId,
            cofId: this.data.infoData.cofId,
            classDate: this.data.infoData.classDate,
            orderNo: this.data.infoData.orderNo,
        }
        const param = JSON.stringify(obj)
        wx.navigateTo({
            url: `/pages/operation/classRecord/classMakeupStudent/index?param=${param}`,
        })

    },

    selectStudent(e) {
        const item = e.currentTarget.dataset.query;
        this.setData({
            showDetail: true,
            'detailForm.id': item.id,
            'detailForm.studentName': item.studentName,
            'detailForm.courseName': item.courseName,
            'detailForm.detailSelectState': item.state,
            'detailForm.classHourPay': item.classHourPay,
            'detailForm.description': item.description
        })
    },

    changeOrder(e) {
        this.setData({
            'detailForm.detailSelectState': e.currentTarget.dataset.query
        })
    },
    onStepChange(e) {
        this.setData({
            'detailForm.classHourPay': e.detail
        })
    },
    confirmTap() {
        const param = {
            id: this.data.detailForm.id,
            classHourPay: this.data.detailForm.classHourPay,
            remark: this.data.detailForm.description,
            state: this.data.detailForm.detailSelectState
        }
        console.log("sss: ", param.state)
        if (param.state == "" || param.state == '1') {
            Toast('请选择到课状态！');
            return;
        }
        classModel.updateOrderByIdApi(param).then(res => {
            classModel.saveOrderUpdateApi({ tid: param.id, type: 1, remark: param.remark, state: param.state }).then(resp => {
                this.setData({
                    showDetail: false,
                    pageNum: 1,
                    tableDatas: []
                })
                this.getMumberListByRollCallId()
            })
        })
    },
    onCloseDetail() {
        this.setData({
            showDetail: false
        })
    },

    // 评价学员
    navigateToEvaluate() {
        wx.navigateTo({
            url: `/pages/operation/classRecord/evaluateDetails/index?id=${this.data.id}`,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },



    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})